package cn.itcast.fruitstore.tools;



import java.sql.*;
import java.util.Properties;


public class JDBCUtils {
    public static String drive;
    public static String url;
    public static String database;
    public static String user;
    public static String password;
    private static Connection cn;



    static {
        Properties properties=new Properties();
        try {

            properties.load(JDBCUtils.class.getResourceAsStream("jdbc.properties"));

            drive = properties.getProperty("drive");
            url = properties.getProperty("url");
            database = properties.getProperty("database");
            user = properties.getProperty("user");
            password = properties.getProperty("password");



        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    //连接测试
    public static Connection getConnetion(){

        try {
            //注册驱动
            Class.forName(drive);
            //连接数据库
            cn= DriverManager.getConnection
                    (url+database,user,password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return cn;

    }

    //释放资源
    public static void Rease(Connection cn, Statement st, ResultSet rt){
        clossCT(cn);
        clossSM(st);
        clossRS(rt);

    }

    //关闭connection连接
    public  static void clossCT(Connection connection){
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                connection=null;
            }

        }
    }
    //关闭Statement连接
    public  static void clossSM(Statement st){
        if (st!=null){
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                st=null;
            }

        }
    }
    //关闭ResultSet连接
    public  static void clossRS(ResultSet rs){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                rs=null;
            }

        }
    }



}
